<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class PositionsController extends BaseController {
	
    public function index(){
    	$this->assign('page_title','职位列表');
    	$userModel = D("Users");
    	$list = $userModel->select();
    	$this->assign('list',$list);
    	$this->display();
    }

    public function getData()
    {
        $DataModel = D("Positions");
        $items = array();
        $page = (int)$_GET['page'];
        $items['page'] = $page>0 ? $page :1;
        $items['records'] = $DataModel->count();
        $limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
        $items['total'] = ceil($items['records']/$limit);
        $list = $DataModel->page($page)->limit($limit)->select();
        $items['rows'] = $list;
    	$this->ajaxReturn($items);
    }

    public function add(){

        $PositionModel = D("Positions");

        if(!empty($_POST)) {
            $_POST['create_time'] = date('Y-m-d H:i:s');
            $_POST['update_time'] = date('Y-m-d H:i:s');

            if(is_array($_POST['xzfl'])){

                foreach ($_POST['xzfl'] as $key => $value){

                    $xzfl.= $value."，";
                }

            }

            $_POST['xzfl']= $xzfl;

            $data = $PositionModel->create($_POST);
            $insert_id = $PositionModel->add($data);
            if($insert_id){
                header("Content-type: text/html; charset=utf-8");
                echo "<script>window.location.href='".__APP__."/index.php/Positions/';</script>";exit;
            }
        }

        $this->display();

    }

    public function edit(){
        $DataModel = D("Positions");


        if(!empty($_POST)) {

            $_POST['update_time'] = date('Y-m-d H:i:s');

            if(is_array($_POST['xzfl'])){

                foreach ($_POST['xzfl'] as $key => $value){

                    $xzfl.= $value."，";
                }

            }
            $_POST['xzfl']= $xzfl;

            $DataModel->where("pos_id=".(int)$_POST['id'])->save($_POST);

            header("Content-type: text/html; charset=utf-8");
            echo "<script>window.location.href='".__APP__."/index.php/Positions/';</script>";exit;
        }else{
            $item = $DataModel->where("pos_id=".(int)$_GET['id'])->select();



            $item[0]['xzfl']= substr($item[0]['xzfl'], 0, -3);

            $item[0]['xzfl']= explode('，',$item[0]['xzfl']);

            $this->assign('vo',$item[0]);
            $this->assign('xzfl',$item[0]['xzfl']);
            $this->display();
        }
    }

    public function delete(){
        if(!empty($_POST)) {
            $DataModel = D("Positions");
            $id = $_POST['id'];
            $result = $DataModel->delete((int)$id);

            if($result){
                header("Content-type: text/html; charset=utf-8");
                echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
            }
        }     $this->display();
    }


}